using System.Globalization;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using System;
using Daemon.Common.Json;
using Daemon.Common.CaptchaHelp;
using Autofac;
using AspectCore.Extensions.Autofac;
using Autofac.Configuration;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Daemon.Model;
using Daemon.Common.SqlParser;
using Microsoft.AspNetCore.Authorization;
using Daemon.Common.Executer;
using Daemon.Common.Cache;
using Daemon.Reflection;
using Daemon.Common.Auth;
using Daemon.Data.Infrastructure.Util;
using Daemon.Repository.EF;
using Daemon.Data.Infrastructure.Configuration;
using DaemonAPI.AppStart;
using DaemonAPI.SchdulerJob;
using Quartz;
using Quartz.Impl;
using Microsoft.AspNetCore.SignalR;
using DaemonAPI.Hubs;

namespace DaemonAPI.Middleware
{
	public static class RegisterExtensions
	{
		public static IServiceCollection Register(this IServiceCollection services)
		{
			//AddSingleton :每次请求都会获取同一个实例
			//AddScoped:每次请求都会获取一个新的实例,同一个请求获取多次的实例相同
			//AddTransient:每次请求都会获取一个新的实例，即使一个请求获取多次也是不同的实例
			services.AddSingleton<IDelimitedListParser, JsonDelimitedListParser>();
			services.AddScoped<IHttpContextAccessor, HttpContextAccessor>();
			services.AddSingleton<ICaptcha, Captcha>();
			services.AddTransient<RelationshipExecutor>();
			services.AddSingleton<IDaemonDistributedCache, DaemonDistributedCache>();
			services.AddSingleton<ISqlDialect, MySQLDialect>();
			services.AddSingleton<ClientAuthCache>();
			services.AddSingleton<SessionItemRepository>();
			services.AddSingleton<IMemoryCacheUtil, MemoryCacheUtil>();
			services.AddSingleton<IContextInfoProvider, ContextInfoProvider>();
			services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
			services.AddTransient<Scheduler>();
			services.AddTransient<UserOrderJob>();
			SetAllowAllOrigin(services);
			return services;
		}

		private static void SetAllowAllOrigin(IServiceCollection services)
		{
			services.AddCors(options =>
			{
				options.AddPolicy("AllowAllOrigin", builder =>
				{
					builder.SetPreflightMaxAge(TimeSpan.FromSeconds(1800L));
					builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
				});
			});
		}

		public static IServiceProvider RegisterAutofac(IServiceCollection services, IConfiguration configuration, IContainer container)
		{
			var builder = new ContainerBuilder();
			builder.RegisterDynamicProxy();
			builder.Populate(services);
			var module = new ConfigurationModule(configuration);
			builder.RegisterModule(module);
			container = builder.Build();
			return new AutofacServiceProvider(container);
		}
	}
}
